package com.yeyks.controller.finance;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.pojo.param.minivipcard.MiniCardWithdrawalParam;
import com.yeyks.commonReference.service.vip.MemberCardEnableOrderService;
import com.yeyks.commonReference.service.vip.MiniCardCommissionOverviewService;
import com.yeyks.commonReference.service.vip.MiniCardWithdrawalOrderService;
import com.yeyks.vip.param.MiniCardWithdrawalOrderCheckParam;
import com.yeyks.vip.param.MiniCardWithdrawalOrderListParam;
import com.yeyks.vip.param.MiniCardWithdrawalOrderUpdateParam;
import com.yeyks.vip.result.MiniCardWithdrawalOrderCheckResult;
import com.yeyks.vip.result.MiniCardWithdrawalOrderDownloadResult;
import com.yeyks.vip.result.MiniCardWithdrawalOrderListResult;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 卖卡佣金提现订单相关接口
 *
 * @author shuai
 * @date 2020-01-07
 */
@Slf4j
@Api(tags = "卖卡佣金提现订单")
@RestController
@RequestMapping("/mini/card/order")
public class MiniCardSellOrderController {
    @Autowired
    private MiniCardWithdrawalOrderService orderService;
    @Autowired
    private MiniCardCommissionOverviewService overviewService;
    @Autowired
    private MemberCardEnableOrderService enableOrderService;

    @NeedToken
    @ApiOperation(value = "v4.1 --> 申请提现", notes = "body中返回true或false")
    @PostMapping("/withdraw")
    public AjaxResponseBody<Boolean> orderWithdraw() {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        overviewService.withdraw(new MiniCardWithdrawalParam(loginUser.getId(), loginUser.getType(), 7, 2));
        return AjaxResponseBody.success(true);
    }

    @SystemLog(value = SysLogMsg.MINI_CARD_UPDATE)
    @NeedToken
    @ApiOperation(value = "v4.1 --> 修改提现订单状态(总公司权限)", notes = "body中返回true或false")
    @PostMapping("/update")
    public AjaxResponseBody<Boolean> orderUpdate(@Validated MiniCardWithdrawalOrderUpdateParam param) {
        return AjaxResponseBody.success(orderService.pass(AccountUtils.getLoginUser(), param));
    }

    @SystemLog(value = SysLogMsg.MINI_CARD_UPDATE_MORE)
    @NeedToken
    @ApiOperation(value = "v4.1 --> 批量修改提现订单状态为审核通过(总公司权限)", notes = "body中返回true或false")
    @ApiImplicitParam(value = "订单号", name = "orderNo", paramType = "query", dataType = "string", required = true)
    @PostMapping("/update/more")
    public AjaxResponseBody<Boolean> orderUpdateMore(String orderNo) {
        Arguments.strIsNull(orderNo, "订单集合不能为空");
        return AjaxResponseBody.success(orderService.myUpdateMore(AccountUtils.getLoginUser(), orderNo));
    }

    @NeedToken
    @ApiOperation(value = "v4.1 --> 提现订单列表", response = MiniCardWithdrawalOrderListResult.class)
    @PostMapping("/list")
    public AjaxResponseBody<PageResult<MiniCardWithdrawalOrderListResult>> orderList(MiniCardWithdrawalOrderListParam param) {
        PageResult<MiniCardWithdrawalOrderListResult> pageResult = orderService.myList(AccountUtils.getLoginUser(), param);
        return AjaxResponseBody.success(pageResult);
    }

    @NeedToken
    @ApiOperation(value = "v4.1 --> 上次提现到这次提现中订单", response = MiniCardWithdrawalOrderListResult.class)
    @PostMapping("/period/order")
    public AjaxResponseBody<PageResult<MiniCardWithdrawalOrderCheckResult>> periodOrder(MiniCardWithdrawalOrderCheckParam param) {
        PageResult<MiniCardWithdrawalOrderCheckResult> pageResult = enableOrderService.periodOrder(AccountUtils.getLoginUser(), param);
        return AjaxResponseBody.success(pageResult);
    }

    @ApiIgnore
    @NeedToken
    @ApiOperation(value = "v4.1 --> 提现订单导出")
    @PostMapping("/download")
    public void orderDownload(HttpServletResponse response, MiniCardWithdrawalOrderListParam param) {
        List<MiniCardWithdrawalOrderDownloadResult> results = orderService.orderDownload(AccountUtils.getLoginUser(), param);
        ExcelUtil.disposeWriteExcelWithSheets(response, results, new MiniCardWithdrawalOrderDownloadResult());
    }

}
